package com.springboot.rabbitmq;

import com.springboot.config.ConsumerConfig;
import com.springboot.rabbitmq.listener.UserRabbitMqListener;
import lombok.extern.log4j.Log4j2;
import org.springframework.amqp.core.AcknowledgeMode;
import org.springframework.amqp.core.AmqpAdmin;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * rabbitMq配置
 *
 * @author supu
 * @create 2019-01-25 10:25
 **/
@Log4j2
@Configuration
public class RabbitMqConfig {
    @Autowired
    private ConsumerConfig consumerConfig;
    @Autowired
    private UserRabbitMqListener userRabbitMqListener;

    /*@Bean
    public ConnectionFactory connectionFactory(){
        CachingConnectionFactory connectionFactory = new CachingConnectionFactory();
        connectionFactory.setAddresses(environment.getProperty("spring.rabbitmq.addresses"));
        connectionFactory.setHost(environment.getProperty("spring.rabbitmq.host"));
        connectionFactory.setUsername(environment.getProperty("spring.rabbitmq.username"));
        connectionFactory.setPassword(environment.getProperty("spring.rabbitmq.password"));
        connectionFactory.setPort(Integer.valueOf(environment.getProperty("spring.rabbitmq.port")));
        connectionFactory.setVirtualHost(environment.getProperty("spring.rabbitmq.virtual-host"));
        // 这里需要显示调用才能进行消息的回调  必须要设置
        connectionFactory.setPublisherConfirms(true);
        return connectionFactory;
    }*/

    @Bean
    public RabbitTemplate rabbitTemplate(ConnectionFactory connectionFactory){
        return new RabbitTemplate(connectionFactory);
    }

    @Bean
    public AmqpAdmin rabbitAdmin(ConnectionFactory connectionFactory){
        return new RabbitAdmin(connectionFactory);
    }

    @Bean
    public Queue queue(AmqpAdmin amqpAdmin){
        Queue queue = new Queue(consumerConfig.getString(ConsumerConfig.RABBITMQ_QUEUE_NAME), true, false, false);
        queue.setAdminsThatShouldDeclare(amqpAdmin);
        return queue;
    }

    @Bean
    public DirectExchange exchange(AmqpAdmin amqpAdmin){
        DirectExchange exchange = new DirectExchange(consumerConfig.getString(ConsumerConfig.RABBITMQ_EXCHANGE_NAME), true, false);
        exchange.setAdminsThatShouldDeclare(amqpAdmin);
        return exchange;
    }

    @Bean
    public Binding directExchangeBinding(Queue queue, DirectExchange directExchange, AmqpAdmin amqpAdmin){
        Binding binding = BindingBuilder.bind(queue).to(directExchange).with(consumerConfig.getString(ConsumerConfig.RABBITMQ_ROUTING_KEY));
        binding.setAdminsThatShouldDeclare(amqpAdmin);
        return binding;
    }

    @Bean
    public SimpleMessageListenerContainer userMessageContainer(ConnectionFactory connectionFactory, Queue queue) {
        SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(connectionFactory);
        container.setQueues(queue);
        container.setExposeListenerChannel(true);
        container.setMaxConcurrentConsumers(1);
        container.setConcurrentConsumers(1);
        // 设置确认模式手工确认
        container.setAcknowledgeMode(AcknowledgeMode.MANUAL);
        container.setMessageListener(userRabbitMqListener);
        return container;
    }
}
